# -*- coding: utf-8 -*-
# Created Time : 2022/1/5 15:50
# Author:Zhou
import os
import h5py
import numpy as np
from ANC_tools import sin_wave, awgn, NPP_1, NPP_2, NPP_3, NPP_4, NPP_5, logisticChaotic
import scipy.io as scio

# 生成交叉验证数据
# some settings
dataFileList = ['C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/volvo_norm.mat',
                'C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/factory1_norm.mat',
                'C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/destroyerengine_norm.mat',
                'C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/babble_norm.mat',
                'C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/buccaneer1_norm.mat',
                'C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/f16_norm.mat',
                'C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/machinegun_norm.mat',
                'C:/Users/francklinson/PycharmProjects/ANC_PY/NoiseData/leopard_norm.mat']

NPP = NPP_4
rms = 1.0
samplingRate = 16000
noiseLength = 3  # 时间 单位s

filepath = 'datasets/hybrid_NPP4/cv/'
filename = 'cv.ex'
n_cv_ex = 16  # 样本数
writer = h5py.File(os.path.join(filepath, filename), 'w')
idx = 0
for dataFile in dataFileList:
    data = scio.loadmat(dataFile)
    noiseSource = data['data']
    for i in range(n_cv_ex):
        # generate a noisy mixture
        # 和训练集 测试集的一样
        print('generating: sample_{}'.format(idx))
        # mix = sin_wave(A=1, f=100, fs=16000, phi=0, t=1) \
        #       + sin_wave(A=0.5, f=100, fs=16000, phi=90, t=1) \
        #       + sin_wave(A=0.2, f=200, fs=16000, phi=180, t=1)
        # mix = logisticChaotic(0.9, 4, 16000)
        # mix = awgn(ref, 40)
        beginning = np.random.randint(0, len(noiseSource) - noiseLength * samplingRate)
        end = beginning + noiseLength * samplingRate
        mix = noiseSource[beginning: end, :]
        sph = NPP(mix)
        # sph = sin_wave(A=1, f=100, fs=16000, phi=0, t=1)
        # normalize
        c = rms * np.sqrt(mix.size / np.sum(mix ** 2))
        mix *= c
        sph *= c

        # 新建组
        writer_grp = writer.create_group(str(idx))
        writer_grp.create_dataset('mix', data=mix.astype(np.float32), shape=mix.shape, chunks=True)
        writer_grp.create_dataset('sph', data=sph.astype(np.float32), shape=sph.shape, chunks=True)
        idx += 1
writer.close()
